package address;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TuGi
 */
public class BeginPanel extends JFrame {

    int k;   //所有联系人数
    int g = 0;  //未分组联系人数
    bgPanel panel = null;
    private JTextField serchText = new JTextField();
    Font font = new Font("微软雅黑", Font.PLAIN, 12);
    Font font2 = new Font("微软雅黑", Font.PLAIN, 16);
    People people = null;
    ArrayList contacts = null;    //保存所有联系人的信息，里面包含的是People的类
    ArrayList belongs = null;    //保存所有存在的分组，包含的是String的类
    ArrayList jbts = new ArrayList();   //保存分组的按钮，包含的是JButton的类
    ArrayList LI = new ArrayList();    //保存分组，包含的是ArrayList的类
    showPeople showpeople = null;
    JButton jbtkeep = new JButton();
    sshowpanel ssp = null;
    PeoplePanel pp[] = new PeoplePanel[50];
    ArrayList L = new ArrayList();

    public BeginPanel(ArrayList contacts, ArrayList belongs) {
        this.contacts = contacts;
        this.belongs = belongs;
        for (int i = 0; i < this.contacts.size() - 1; i++) {
            for (int j = i + 1; j < this.contacts.size(); j++) {
                People people1 = (People) this.contacts.get(i);
                People people2 = (People) this.contacts.get(j);
                if (Collator.getInstance(Locale.CHINESE).compare(people1.name, people2.name) > 0) {
                    this.contacts.set(i, people2);
                    this.contacts.set(j, people1);
                }
            }
        }
        k = contacts.size();

        for (int i = 0; i < k; i++) {
            people = (People) contacts.get(i);
            if (people.belongs.isEmpty()) {
                g++;
            }
        }
        for (int i = 0; i < belongs.size(); i++) {
            String s = (String) belongs.get(i);
            ArrayList li = new ArrayList();         //保存分组的联系人，里面包含的事People的类
            li.add(s);
            for (int j = 0; j < k; j++) {
                people = (People) contacts.get(j);
                if (people.belongs.contains(s)) {
                    li.add(people);
                }
            }
            LI.add(li);
        }

        panel = new bgPanel();
        panel.setLayout(null);
        add(panel);

        JLabel JLabel1 = new JLabel("联系人");
        JLabel1.setFont(font);
        JLabel1.setForeground(Color.gray);
        JLabel1.setBounds(20, 10, 157, 20);
        add(JLabel1);

        JButton alljbt = new JButton("+所有联系人" + "(" + k + ")");  //所有联系人jbt
        alljbt.setFont(font);
        alljbt.setForeground(Color.black);
        alljbt.setContentAreaFilled(false);
        //alljbt.setBorderPainted(false);
        alljbt.setBounds(20, 35, 157, 30);
        add(alljbt);

        JButton nonjbt = new JButton("+未分组联系人" + "(" + g + ")");   //未分组联系人jbt
        nonjbt.setFont(font);
        nonjbt.setForeground(Color.black);
        nonjbt.setContentAreaFilled(false);
        //nonjbt.setBorderPainted(false);
        nonjbt.setBounds(20, 82, 157, 30);
        add(nonjbt);

        ImageIcon Icon1 = new ImageIcon("img/newContact.png");  //增加联系人jbt
        JButton newContact = new JButton(Icon1);
        newContact.setBorderPainted(false);
        newContact.setBounds(210, 35, 120, 30);

        ImageIcon Icon4 = new ImageIcon("img/newGroup.png");    //增加分组jbt
        JButton newGroup = new JButton(Icon4);
        newGroup.setBorderPainted(false);
        newGroup.setBounds(360, 35, 120, 30);

        ImageIcon Icon5 = new ImageIcon("img/delGroup.png");     //删除分组jbt
        JButton delGroup = new JButton(Icon5);
        delGroup.setBorderPainted(false);
        delGroup.setBounds(510, 35, 120, 30);

        ImageIcon IconIn = new ImageIcon("img/import.png");
        JButton inCon = new JButton(IconIn);
        inCon.setBorderPainted(false);
        inCon.setBounds(210, 500, 120, 30);

        ImageIcon IconOut = new ImageIcon("img/export.png");
        JButton outCon = new JButton(IconOut);
        outCon.setBorderPainted(false);
        outCon.setBounds(360, 500, 120, 30);

        ImageIcon Icon6 = new ImageIcon("img/search.png");    //搜索jbt
        JButton serchContact = new JButton(Icon6);
        serchContact.setBorderPainted(false);
        serchContact.setBounds(790, 80, 45, 30);

        JLabel enter = new JLabel("搜索所有联系人:");
        enter.setFont(font2);
        enter.setForeground(Color.black);
        enter.setBounds(210, 80, 150, 30);

        serchText.setForeground(Color.black);         //文本域
        serchText.setHorizontalAlignment(JTextField.CENTER);
        serchText.setBounds(345, 80, 430, 30);

        ImageIcon Icon7 = new ImageIcon("img/save.png");    //保存联系信息
        JButton jbtsave = new JButton(Icon7);
        jbtsave.setBorderPainted(false);
        jbtsave.setBounds(710, 500, 120, 30);
        jbtsave.addActionListener(new saveListener());

        add(newContact);
        add(newGroup);
        add(delGroup);
        add(enter);
        add(serchText);
        add(serchContact);
        add(jbtsave);
        add(inCon);
        add(outCon);
        AllListener listener1 = new AllListener();
        NonListener listener2 = new NonListener();
        newConListener Listener3 = new newConListener();  //监听器

        newGroListener Listener6 = new newGroListener();
        delGroListener Listener7 = new delGroListener();
        serchConListener Listener8 = new serchConListener();
        inConListener Listener9 = new inConListener();
        outConListener Listener10 = new outConListener();

        alljbt.addActionListener(listener1);  //注册“所有联系人”按钮监听器
        nonjbt.addActionListener(listener2); //注册“未分组联系人”按钮监听器
        newContact.addActionListener(Listener3);//注册监听器(创建新联系人）
        newGroup.addActionListener(Listener6);
        delGroup.addActionListener(Listener7);
        serchContact.addActionListener(Listener8);
        inCon.addActionListener(Listener9);
        outCon.addActionListener(Listener10);

//   *根据具体分组显示*
        JPanel groupPanel2 = new JPanel();    //分组面板2                           
        groupPanel2.setLayout(null);
        groupPanel2.setOpaque(false);
        JScrollPane jscp1 = new JScrollPane();
        jscp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        groupPanel2.setPreferredSize(new Dimension(150, 800));
        jscp1.setViewportView(groupPanel2);
        jscp1.setBounds(20, 130, 157, 450);
        jscp1.setOpaque(false);
        jscp1.getViewport().setOpaque(false);
        add(jscp1, 0);
        JButton jbtBelong = null;//每个分组的按钮
        int h = 20;
        for (int i = 0; i < belongs.size(); i++) {
            String s = (String) belongs.get(i);
            jbtBelong = new JButton(s);
            jbtBelong.setBounds(15, h, 100, 30);
            jbtBelong.setFont(font2);
            jbtBelong.setForeground(Color.black);
            jbtBelong.setContentAreaFilled(false);
            jbtBelong.addActionListener(new jbtBelongActionListener());
            h = h + 40;
            jbts.add(jbtBelong);
            groupPanel2.add(jbtBelong);
        }

        showpeople = new showPeople(contacts);

        add(showpeople.jscp, 0);
        add(panel, -1);
    }

    public static void main(String[] args) throws ClassNotFoundException {
        File f1 = new File("belongs.txt");
        File f2 = new File("contacts.txt");
        ArrayList contacts = new ArrayList();
        ArrayList belongs = new ArrayList();
        People people = null;
        ObjectInputStream input = null;
        if (f2.exists()) {
            try {
                input = new ObjectInputStream(new FileInputStream(f2));
                for (int i = 0; i < getContNum("contacts.txt"); i++) {
                    People p = (People) (input.readObject());
                    contacts.add(p);
                }
            } catch (IOException ex) {
                System.out.println("读取文件出现错误");
            }

        }
        if (f1.exists()) {

            try {
                input = new ObjectInputStream(new FileInputStream(f1));
                for (int i = 0; i < getbelongNum("belongs.txt"); i++) {
                    String s = (String) (input.readObject());
                    belongs.add(s);
                }

            } catch (IOException ex) {
                System.out.println("读取文件出现错误");
            }

        }
        BeginPanel frame = new BeginPanel(contacts, belongs);
        frame.setSize(900, 700);
        frame.setTitle("欢迎使用通讯录管理系统");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    public static int getContNum(String file) throws ClassNotFoundException {
        File f = new File(file);
        int n = 0;
        ObjectInputStream input = null;

        try {
            input = new ObjectInputStream(new FileInputStream(f));
            while (true) {
                People p = (People) (input.readObject());
                n++;
            }
        } catch (IOException ex) {
            return n;
        }

    }

    public static int getbelongNum(String file) throws ClassNotFoundException {
        File f = new File(file);
        int n = 0;
        ObjectInputStream input = null;

        try {
            input = new ObjectInputStream(new FileInputStream(f));
            while (true) {
                String ss = (String) (input.readObject());
                n++;
            }
        } catch (IOException ex) {
            return n;
        }

    }

    private class outConListener implements ActionListener {

        public outConListener() {
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            ExportCon frame = new ExportCon(contacts);
            frame.setSize(300, 300);
            frame.setTitle("导出通讯录");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }

    private class inConListener implements ActionListener {

        public inConListener() {
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            ImportCon frame = new ImportCon(contacts,belongs);
            frame.setSize(300, 300);
            frame.setTitle("导入通讯录");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            dispose();
        }
    }

    private class saveListener implements ActionListener {

        public saveListener() {
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            try {
                new Outputpeople(contacts, belongs);
            } catch (IOException ex) {
                Logger.getLogger(BeginPanel.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    class showPeople extends JPanel {

        JScrollPane jscp = new JScrollPane();
        //PPeoplePanel j = null;

        public showPeople(ArrayList List) {
            //setLayout(null);
            L = List;
            ssp = new sshowpanel();
            ssp.setLayout(null);
            ssp.setOpaque(false);
            jscp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            ssp.setPreferredSize(new Dimension(600, 800));
            jscp.setViewportView(ssp);
            jscp.setBounds(210, 130, 630, 350);
            jscp.setOpaque(false);
            jscp.getViewport().setOpaque(false);
            add(jscp, 0);

        }

        void changeList(ArrayList ll) {
            L = ll;
            ssp.repaint();
        }

    }

    class sshowpanel extends JPanel {

        PeoplePanel j = null;
        JLabel tipname = new JLabel("姓名");
        JLabel tipemail = new JLabel("电子邮件");
        JLabel tipphone = new JLabel("联系号码");
        JLabel tipoption = new JLabel("操作");

        sshowpanel() {

        }

        public void refresh() {
            repaint();
        }

        public void reclear() {
            removeAll();
        }

        protected void paintComponent(Graphics g) {
            int H = 40;
            super.paintComponent(g);
            tipname.setBounds(10, 0, 150, 30);
            tipname.setFont(font2);
            tipname.setOpaque(false);
            ssp.add(tipname);

            tipemail.setBounds(170, 0, 150, 30);
            tipemail.setFont(font2);
            tipemail.setOpaque(false);
            ssp.add(tipemail);

            tipphone.setBounds(330, 0, 150, 30);
            tipphone.setFont(font2);
            tipphone.setOpaque(false);
            ssp.add(tipphone);
            
            tipoption.setBounds(500, 0, 150, 30);
            tipoption.setFont(font2);
            tipoption.setOpaque(false);
            ssp.add(tipoption);
            for (int i = 0; i < L.size(); i++) {
                j = new PeoplePanel((People) L.get(i), contacts, belongs);
                j.panel.setBounds(10, H, 600, 30);
                j.edit.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        dispose();
                    }

                });
                j.delet.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        dispose();

                    }

                });
                ssp.add(j.panel);
                H = H + 40;
            }

        }
    }

    class serchConListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String message = serchText.getText();
            ArrayList l = null;
            if (message.length() != 0) {
                    SearchCon search = new SearchCon(message, contacts);
                    l = search.getsearchresult();
                    ssp.reclear();
                    showpeople.changeList(l);         
            }
        }
    }

    class delGroListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            DeletGroup frame = new DeletGroup(contacts, belongs);
            frame.setBackground(Color.white);
            frame.setSize(400, 400);
            frame.setTitle("删除分组");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            dispose();
        }
    }

    class newGroListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            NewGroup frame = new NewGroup(contacts, belongs);
            frame.setSize(300, 220);
            frame.setTitle("新建分组");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            dispose();
        }
    }

    class newConListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            CreatPeople frame = new CreatPeople(contacts, belongs);
            frame.setSize(500, 600);
            frame.setTitle("创建新的联系人");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            dispose();
        }
    }

    class AllListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            ssp.reclear();
            showpeople.changeList(contacts);
        }

    }

    class NonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            ArrayList leftcon = new ArrayList();
            for (int i = 0; i < contacts.size(); i++) {
                People p = (People) contacts.get(i);
                if (p.belongs.isEmpty()) {
                    leftcon.add(p);
                }
            }
            ssp.reclear();
            showpeople.changeList(leftcon);
        }
    }

    class jbtBelongActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            String s = ae.getActionCommand();
            ArrayList L = null;
            ArrayList l = new ArrayList();
            for (int i = 0; i < LI.size(); i++) {
                L = (ArrayList) LI.get(i);
                if (s.compareTo((String) L.get(0)) == 0) {
                    break;
                }
            }

            for (int i = 1; i < L.size(); i++) {
                People people = (People) L.get(i);
                l.add(people);
            }
            ssp.reclear();
            showpeople.changeList(l);

        }
    }

    class bgPanel extends JPanel {

        ImageIcon bg;

        @Override
        public void paint(Graphics g) {
            super.paintComponent(g);
            bg = new ImageIcon("img/bg.jpg");
            g.drawImage(bg.getImage(), 0, 0, null);
        }

    }

}
